﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data.EntityClient;
using System.Diagnostics;

namespace EAIClassLibrary
{
    public static class LogManager
    {

        #region public

        public static void ShowMessage(string source, XmlDocument sourceXml)
        {
            System.Diagnostics.EventLog.WriteEntry(source, sourceXml.OuterXml.ToMax());
        }

        public static void ShowMessage(string source, string sourceStr)
        {
            var tmp = sourceStr.ToMax();
            System.Diagnostics.EventLog.WriteEntry(source, tmp);
        }

        /// <summary>
        /// Add Request Log
        /// </summary>
        /// <param name="sourceXml"></param>
        public static void AddRequestLog(XmlDocument sourceXml)
        {
            var msgType = "Request";
            AddLog(sourceXml, msgType);
        }

        /// <summary>
        /// Add ResponseLog
        /// </summary>
        /// <param name="sourceXml"></param>
        public static void AddResponseLog(XmlDocument sourceXml)
        {
            var msgType = "Response";
            AddLog(sourceXml, msgType);
        }

        public static string ToMax(this string source)
        {
            if (source != null)
            {
                if (source.Length > 3000)
                {
                    return source.Substring(0, 3000);
                }
            }
            return source;
        }

        #endregion

        #region private
        /// <summary>
        /// AddLog
        /// </summary>
        /// <param name="sourceXml"></param>
        /// <param name="msgType"></param>
        private static void AddLog(XmlDocument sourceXml, string msgType)
        {
            //Init Log
            Table_Log log = new Table_Log()
            {
                SourceXML = sourceXml.InnerXml,
                MsgType = msgType,
                LogDateTime = DateTime.Now
            };
            string root = "ESBEnvelope";
            if (msgType == "Response") { root = "ESBEnvelopeResult"; }

            var esbEnvolope = sourceXml[root, "http://MetLifeEAI.EAISchema"];
            for (int j = 0; j < esbEnvolope.ChildNodes.Count; j++)
            {
                var esbItem = esbEnvolope.ChildNodes[j];

                //ESB Header
                if (esbItem.Name == "ESBHeader")
                {
                    for (int i = 0; i < esbItem.ChildNodes.Count; i++)
                    {
                        var node = esbItem.ChildNodes[i];

                        if (node.Name == "ESBHdVer") { log.ESBHdVer = node.InnerXml; }
                        if (node.Name == "SrvDate") { log.SrvDate = node.InnerXml; }
                        if (node.Name == "SrvTime") { log.SrvTime = node.InnerXml; }
                        if (node.Name == "SenderID") { log.SenderID = node.InnerXml; }
                        if (node.Name == "ReceiverID") { log.ReceiverID = node.InnerXml; }
                        if (node.Name == "SrvOpName") { log.SrvOpName = node.InnerXml; }
                        if (node.Name == "SrvOpVer") { log.SrvOpVer = node.InnerXml; }
                        if (node.Name == "MsgID") { log.MsgID = node.InnerXml; }
                        if (node.Name == "CorrID") { log.CorrID = node.InnerXml; }
                        if (node.Name == "ESBRspCode") { log.ESBRspCode = node.InnerXml; }
                        if (node.Name == "ESBRspDec") { log.ESBRspDec = node.InnerXml; }
                        if (node.Name == "ResField1") { log.ResField1 = node.InnerXml; }
                        if (node.Name == "ResField2") { log.ResField2 = node.InnerXml; }
                        if (node.Name == "ResField3") { log.ResField3 = node.InnerXml; }
                        if (node.Name == "ResField4") { log.ResField4 = node.InnerXml; }
                        if (node.Name == "ResField5") { log.ResField5 = node.InnerXml; }
                    }
                }

                if (esbItem.Name == "MsgBody")
                {
                    //ESB Body
                    log.MsgBody = esbItem.InnerXml;
                }
            }
            Entities context = new Entities("metadata=res://EAIClassLibrary/EAIDBModel.csdl|res://EAIClassLibrary/EAIDBModel.ssdl|res://EAIClassLibrary/EAIDBModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.;Initial Catalog=EAIDB;Persist Security Info=True;User ID=sa;Password=password01!;MultipleActiveResultSets=True'");
            context.AddToTable_Log(log);
            context.SaveChanges();
        }
        #endregion
    }
}
